package com.hongdee.scm.chooseCompany.service.impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.chooseCompany.dao.ITScmChooseCompanyDao;
import com.hongdee.scm.chooseCompany.service.ITScmChooseCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/15.
 */
@Service
public class TScmChooseCompanyServiceImpl implements ITScmChooseCompanyService{

    @Autowired
    private ITScmChooseCompanyDao itScmChooseCompanyDao;

    /**
     * 获取所有启用的品牌
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> getBrand() throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();

        sql.append("select * from t_bc_organ where o_type = '1' and o_p_id in (select id from t_bc_organ where o_type='1' and o_p_id='0') ");

        List<Map<String, Object>> mapList = this.itScmChooseCompanyDao.list(sql.toString(),params.toArray());
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            temp.put("label", map.get("id"));
            temp.put("value",map.get("oName"));
            resList.add(temp);
        }

        return resList;
    }

    /**
     * 获取区域
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> getPlace() throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("select code,label from T_BC_NEWDICT where p_code = 'area' ");

        List<Map<String, Object>> mapList = this.itScmChooseCompanyDao.list(sql.toString(),params.toArray());
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            temp.put("label", map.get("code"));
            temp.put("value",map.get("label"));
            resList.add(temp);
        }
        return resList;
    }

    /**
     * 根据品牌或者区域选公司
     * @param type
     * @param codes
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String,Object>> getCompanyByBrandOrPlace(String type,String[] codes,String uid,String isInvoicing) throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("select  id,o_short_name oName from t_bc_organ where o_type ='1' and id in (");
        List<Map<String,Object>> companyMaps= TokenUtil.getDataCompany(uid);
        for(int i=0;i<companyMaps.size();i++){
            sql.append("?");
            Map infoMap=companyMaps.get(i);
            String id=String.valueOf(infoMap.get("id"));
            params.add(id);
            if(i<(companyMaps.size()-1)){
                sql.append(" , ");
            }
        }
        sql.append(" ) ");
        if(!type.equals("")&&type!=null&&codes.length!=0&&codes!=null){
            if(isInvoicing.equals("0")){//进销存
                if(type.equals("1")){ //品牌
                    sql.append(" and o_p_id in (  ");
                    for(Object id : codes){
                        sql.append("? ,");
                        params.add(id);
                    }
                    sql.append(" '')");
                }
            }else{//非进销存
                if(type.equals("1")){ //品牌
                    sql.append(" and o_car_brand in (  ");
                    for(Object id : codes){
                        sql.append("? ,");
                        params.add(id);
                    }
                    sql.append(" '')");
                }
            }

            if(type.equals("2")){//区域
                sql.append(" and o_area in (  ");
                for(Object id : codes){
                    sql.append("? ,");
                    params.add(id);
                }
                sql.append(" '')");
            }

        }

        return itScmChooseCompanyDao.list(sql.toString(),params.toArray());
    }

    /**
     * 根据id查询公司
     * @param companyId
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> getCompanyName(String[] companyId) throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        if(companyId.length<=0||companyId==null){
            throw new Exception("请选择公司!");
        }
        sql.append("select o_name from t_bc_organ where id in (");
        for(Object id : companyId){
            sql.append("? ,");
            params.add(id);
        }
        sql.append(" '')");
        sql.append(" order by o_name asc");

        List<Map<String, Object>> mapList = this.itScmChooseCompanyDao.list(sql.toString(),params.toArray());
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            temp.put("value",map.get("oName"));
            resList.add(temp);
        }

        return resList;
    }

    @Override
    public List getBrandAll(String uid) throws Exception{

        StringBuffer sqlstr = new StringBuffer();
        List<String> params = new ArrayList<>();
        List brandList = new ArrayList();
        String cid = TokenUtil.getCompanyId(uid);
        //登录人下的所有公司
        List<Map<String,Object>> companyMaps= TokenUtil.getDataCompany(uid);
        //查询这些公司所属的品牌
        sqlstr.append("select id,o_short_name from t_bc_organ where o_disp_order <>'0'" +
                " and o_disp_order <> '1' and id in (" +
                " select o_p_id from t_bc_organ where id in (");
        for(int i=0;i<companyMaps.size();i++){
            sqlstr.append("?");
            Map infoMap=companyMaps.get(i);
            String id=String.valueOf(infoMap.get("id"));
            params.add(id);
            if(i<(companyMaps.size()-1)){
                sqlstr.append(" , ");
            }
        }
        sqlstr.append("))");
        List<Map<String,Object>> brands = this.itScmChooseCompanyDao.list(sqlstr.toString(),params.toArray());
        for(Map<String,Object> b:brands){
            Map brand = new HashMap();
            brand.put("code",b.get("id"));
            brand.put("label",b.get("oShortName"));
            brandList.add(brand);
        }
        return brandList;
    }
}
